package com.simple.piger.controller;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.google.common.collect.Lists;
import com.simple.piger.dto.ComboRespDto;
import com.simple.piger.dto.req.InviteCompanyReqDto;
import com.simple.piger.dto.req.JobTitleReqDto;
import com.simple.piger.dto.resp.JobTitleRespDto;
import com.simple.piger.entity.JobTitle;
import com.simple.piger.service.JobTitleService;
import com.simple.piger.utils.AjaxResult;
import com.simple.piger.utils.DateUtils;
import com.simple.piger.utils.PublicUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 职位 前端控制器
 * </p>
 *
 * @author baolw
 * @since 2020-02-06
 */
@RestController
@RequestMapping("/jobTitle")
public class JobTitleController extends BaseController {
    @Autowired
    private JobTitleService jobTitleService;

    /**
     * 运营平台查看职位的接口
     * @param hashMap
     * @return
     */
    @GetMapping("/list")
    public AjaxResult list(@RequestParam HashMap hashMap) {
        JSONObject jsonObject = PublicUtil.getJSONObject(hashMap);
        Integer page = jsonObject.getInteger("page");
        Integer level = jsonObject.getInteger("level");
        Integer limit = jsonObject.getInteger("limit");
        List<JobTitleRespDto> list = jobTitleService.list(page, limit, level);
        int count = jobTitleService.count(level);
        return success(count, list);
    }

    /**
     * 运营平台查看职位详情的接口
     * @param id
     * @return
     */
    @GetMapping("/detail/{id}")
    public AjaxResult detail(@PathVariable("id") String id) {
        JobTitle jobTitle = jobTitleService.selectById(id);
        JobTitleRespDto dto = new JobTitleRespDto();
        BeanUtils.copyProperties(jobTitle, dto);
        dto.setCreateDate(DateUtils.dateFormat(dto.getCreateTime(), "yyyy-MM-dd HH:mm:ss"));
        return success(dto);
    }

    @GetMapping("/selectCombo/{level}")
    public AjaxResult selectCombo(@PathVariable("level") Integer level, @RequestParam(name = "parentId", required = false) String parentId) {
        EntityWrapper<JobTitle> ew = new EntityWrapper<>();
        if (null != level) {
            ew.eq("level", level);
        }
        if (StringUtils.isNotBlank(parentId)) {
            ew.eq("parent_id", parentId);
        }
        List<JobTitle> list = jobTitleService.selectList(ew);
        List<ComboRespDto> results = Lists.newArrayList();

        for (JobTitle title : list) {
            ComboRespDto dto = new ComboRespDto();
            dto.setCode(title.getId());
            dto.setValue(title.getName());
            if (StringUtils.isNotBlank(title.getParentId())) {
                JobTitle job = jobTitleService.selectById(title.getId());
                if (null != job) {
                    dto.setAdditionValue(job.getName());
                }
            }
            results.add(dto);
        }
        return success(results);
    }

    @PostMapping("/saveOrUpdate")
    public AjaxResult saveOrUpdate(@RequestParam HashMap hashMap) {
        JSONObject jsonObject = PublicUtil.getJSONObject(hashMap);
        JobTitleReqDto reqDto = JSONObject.toJavaObject(jsonObject, JobTitleReqDto.class);
        jobTitleService.saveOrUpdate(reqDto);
        return success();
    }

    @GetMapping("/judge/{id}")
    public AjaxResult judge(@PathVariable("id") String id) {
        EntityWrapper<JobTitle> ew = new EntityWrapper<>();
        ew.eq("parent_id", id);
        List<JobTitle> list = jobTitleService.selectList(ew);
        if (list.size() > 0) {
            return success(true);
        }
        return success(false);
    }
    @DeleteMapping("/delete/{id}")
    public AjaxResult delete(@PathVariable("id") String id) {
        JobTitle jobTitle = jobTitleService.selectById(id);
        jobTitle.setDeleteFlag("1");
        jobTitle.setUpdateTime(new Date());
        jobTitle.updateById();
        return success();
    }

}
